package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 当月预约信息展示
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(Integer year, Integer month) {
        int daysInMonth = getDaysInMonth(year, month);
        //构造日期范围
        String startDate = String.format("%d-%d-1", year, month);
        String endDate = String.format("%d-%d-"+daysInMonth, year, month);
        return orderSettingDao.getOrderSettingByMonth(startDate,endDate);
    }

    /**
     * 可预约单独设置
     * @param orderDate
     * @param number
     */
    @Override
    @Transactional
    public void editNumberByDate(Date orderDate, Integer number) {
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderDateStr = sdf.format(orderDate);

        //查询是否当天是否有安排
        OrderSetting orderSetting = orderSettingDao.selectByOrderDate(orderDateStr);
        if(orderSetting!=null){
           //如果有数据就修改
           orderSettingDao.editNumberById(orderSetting.getId(),number);
       }else {
           //否则就新增
           orderSettingDao.insertOrderDate(orderDateStr,number);
       }
    }

    /**
     * 批量插入
     * @param orderSettings
     */
    @Override
    public void addAll(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            editNumberByDate(orderSetting.getOrderDate(), orderSetting.getNumber());
        }

    }

    /**
     * 判断每个月的天数
     * @param year
     * @param month
     * @return
     */
    public static int getDaysInMonth(int year, int month) {
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("月份必须在1到12之间");
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (isLeapYear(year)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                throw new IllegalArgumentException("未知的月份");
        }
    }

    /**
     * 判断是否闰年平年
     * @param year
     * @return
     */
    public static boolean isLeapYear(int year) {
        if (year % 4 != 0) {
            return false;
        } else if (year % 100 != 0) {
            return true;
        } else if (year % 400 != 0) {
            return false;
        } else {
            return true;
        }
    }
}

